home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C023A.ZIP / PART2 / ZOPT4.C < prev    next >
Text File  |  1990-01-31  |  4KB  |  191 lines

  1. /*
  2.  * zopt - five pass optimiser for small-C  (fourth pass)
  3.  *        v2.0 - uses independent processes
  4.  *
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "zopt.h"
  11.  
  12. /*
  13.  * uncomma - split string with two tokens separated by commas
  14.  */
  15. uncomma(string, token1, token2)
  16. char *string, *token1, *token2 ;
  17. {
  18.     int i;
  19.  
  20.     while ( *string != ',' )
  21.         *token1++ = *string++ ;
  22.     *token1 = 0 ;
  23.     ++string ;
  24.     i = 0 ;
  25.     while ( (*string != NULL) & (i < 19) ) {
  26.         *token2++ = *string++ ;
  27.         ++i ;
  28.     }
  29.     *token2 = 0 ;
  30. }
  31.  
  32. /*
  33.  * process conditional jump round unconditional call/jump
  34.  */
  35. jumps(s1, s2, s3, save, last, this, next)
  36. char *s1 ;            /* conditional */
  37. char *s2 ;            /* unconditional */
  38. char *s3 ;            /* replacement */
  39. int *save ;            /* counter for saved bytes */
  40. char **last ;
  41. char **this ;
  42. char **next ;
  43. {
  44.     char line[LINELEN], *temp, *tail ;
  45.  
  46.     /* jump on non-zero round unconditional call */
  47.     if ( tail=match(s1, *last) ) {
  48.       if ( tail=match(tail, *next) ) {
  49.         if ( *tail == ':' ) {
  50.           if ( tail=match(s2, *this) ) {
  51.             /* check there isn't a condition after JP/CALL */
  52.             if (islower(*tail)) {
  53.               strcpy(line, s3) ;
  54.               strcat(line, tail) ;
  55.               strcpy(*last, line) ;
  56.               temp = *this ;
  57.               *this = *next ;
  58.               *next = temp ;
  59.               p_read(*next) ;
  60.               ++(*save) ;
  61.             }
  62.           }
  63.         }
  64.       }
  65.     }
  66. }
  67.  
  68. pass4()
  69. {
  70.     int i, saved[7] ;
  71.     char source1[20], source2[20], dest1[20], dest2[20] ;
  72.     char line[LINELEN] ;
  73.     char *tail, *tail2, *temp ;
  74.     char *last, *this, *next ;
  75.  
  76.     /* start pass 5 */
  77.     switch_down(1) ;
  78.  
  79.     saved[0] = saved[1] = saved[2] = saved[3] = saved[4] = 0 ;
  80.     saved[5] = saved[6] = 0 ;
  81.  
  82.     last = alloc(LINELEN) ;
  83.     this = alloc(LINELEN) ;
  84.     next = alloc(LINELEN) ;
  85.  
  86.     p_read(last) ;
  87.     p_read(this) ;
  88.  
  89.     while ( p_read(next) ) {
  90.  
  91.  
  92.         /* ld hl, ex, ld hl -> ld de, ld hl */
  93.         if ( tail=match(Ldhl,last) ) {
  94.           if ( strcmp(Exdehl,this) == 0 ) {
  95.             if ( match(Ldhl,next) ) {
  96.             if ( *tail != '(' ) {
  97.                 strcpy( line, Ldde ) ;
  98.                 strcat( line, tail ) ;
  99.                 strcpy( last, line ) ;
  100.                 temp = this ;
  101.                 this = next ;
  102.                 next = temp ;
  103.                 p_read( next ) ;
  104.                 ++saved[0] ;
  105.             }
  106.             }
  107.           }
  108.         }
  109.  
  110.         /* jump on zero round unconditional jump */
  111.         jumps("\tJP Z,", "\tJP ", "\tJP NZ,", &saved[1], &last, &this, &next) ;
  112.  
  113.         /* jump on non-zero round unconditional jump */
  114.         jumps("\tJP NZ,", "\tJP ", "\tJP Z,", &saved[1], &last, &this, &next) ;
  115.  
  116.         /* store followed by load */
  117.         if ( tail=match("\tLD ",this) ) {
  118.           if ( tail2=match("\tLD ",next) ) {
  119.             uncomma( tail, dest1, source1 ) ;
  120.             uncomma( tail2, dest2, source2 ) ;
  121.             if ( strcmp(dest1,source2) == 0 ) {
  122.               if ( strcmp(dest2,source1) == 0 ) {
  123.                 p_read( next ) ;
  124.                 ++saved[2] ;
  125.               }
  126.             }
  127.           }
  128.         }
  129.  
  130.         /* jump on zero round unconditional call */
  131.         jumps("\tJP Z,", "\tCALL ", "\tCALL NZ,", &saved[3], &last, &this, &next) ;
  132.  
  133.         /* jump on non-zero round unconditional call */
  134.         jumps("\tJP NZ,", "\tCALL ", "\tCALL Z,", &saved[3], &last, &this, &next) ;
  135.  
  136.         /* two succeeding unconditional jumps */
  137.         if ( tail=match("\tJP ",this) ) {
  138.             if (islower(*tail)) {
  139.                 if ( match("\tJP ",next) ) {
  140.                     if (islower(*tail)) {
  141.                         p_read(next) ;
  142.                         ++saved[4] ;
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.  
  148.         /* CALL ; RET ->  JP */
  149.         /* (only for well-behaved routines) */
  150.         if ( tail=match("\tCALL ",this) ) {
  151.           if ( strcmp("\tRET",next) == 0 ) {
  152.             if ( *tail == 'q' ) {
  153.               strcpy(line,"\tJP " ) ;
  154.               strcat(line,tail) ;
  155.               strcpy(this,line);
  156.               p_read(next);
  157.               ++saved[5] ;
  158.             }
  159.           }
  160.         }
  161.  
  162.         c_write( last ) ;
  163.         temp = last ;
  164.         last = this ;
  165.         this = next ;
  166.         next = temp ;
  167.         if (cpm(CONIN, 255) == CTRLC) exit() ;
  168.     }
  169.     c_write( last ) ;
  170.     c_write( this ) ;
  171.  
  172.     puts("LD HL;EX;LD HL > LD DE;LD HL: "); putdec(saved[0]) ;
  173.     putchar('\n') ;
  174.     puts("JP Z/NZ round JP:             "); putdec(saved[1]) ;
  175.     putchar('\n') ;
  176.     puts("JP Z/NZ round CALL:           "); putdec(saved[3]) ;
  177.     putchar('\n') ;
  178.     puts("Store followed by load:       "); putdec(saved[2]) ;
  179.     putchar('\n') ;
  180.     puts("Two unconditional jumps:      "); putdec(saved[4]) ;
  181.     putchar('\n');
  182.     puts("CALL; RET -> JP:              "); putdec(saved[5]) ;
  183.     putchar('\n') ;
  184.     putchar('\n') ;
  185.     i = saved[0] + saved[1]*3 + saved[2]*2 + saved[3]*3 + saved[4]*3 ;
  186.     i += saved[5] ;
  187.     pr_total(i);
  188.  
  189.     Total += i ;
  190. }
  191.